package cn.genmer.test.security.alibaba.alipay;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

// 未成功
@RestController
@RequestMapping("/alipay2")
@Slf4j
public class AlipayController {
    // 支付宝公钥
    private String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtRYBhTIjwf5lFI6eeTfJEwDvLcryZXouLKXNTnpXwvpvmROTuy322hj8q8flUGHpG4I4hyrUfZdFGqTMf026XFACT2runwJRo4Rux4KXEs+y7j6eTqqXgoDfbtKUCaTW0xIdG/4sHI5a+gTs5Iis1Ncl3CJ4XLtT+dBBG8DpwxuUrYhdUAOmbVE+/9ghjunG49lhJ91gxVsHxNWzVv/FG9wUMLLkISW4KSzMdhms6k1GpbQLzPu8RvCsQNm5rm33QerQ6ECVXYPZPIhRCBzMSeSwE0Ia58MbhS2phM1jldVj+dzuASuDQ70PodBZbGaE1NS9AnFQpw/4aUa/0KeOoQIDAQAB";
    private String appId = "2021000118625951";
    // 应用私钥
    private String privateKey = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCeOdVWUrNVujtz07yGLCeec5sAmdSjRqDyQdM4kEIWcn0SpkNZeuSJ9PM9PB2Cskk5PS7euxbr0GNp1VrBhQS+mChoXoNLqBjbPpEUvlJERw1u8nBuRJf+bGgbTun+4ZVMKslwidYn6ovqbsiqWyvTF51aocKEL9pgrUFuDIu4w5x4sWS7+iQfZ7n0ApOKq8ANefiwEX1btnn3ECZcp+M9Z+uD7mXjYXkPHoYX7dMdDP1+qQCq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Q0+KY5sNaWDG68q6K+NJEDwrzBDJMexKKTJs9IXZxbz68UFJLc6hoTbrSDYp1Q5lvk3IECgYBcA5KpuE8YGYGYFHpu0ZTkfNm7/ExtsHr9ozHUzJRc/1jOYgFPJwWXDXSYJMEpM9BbQ5p36bfjY9S4CtMTQuG2yXVlvuORxMj0C0/plQqhUIvX2/czr/Zr0Gh9gfJxBCejCK/zQyKZ4nTewCoviaSmjEdRKt4W27x+ukn6ko+XJw==";
    @RequestMapping("/pay")
    public String pay(@RequestParam(value = "amt") String amt,
                      @RequestParam(value = "subject") String subject,
                      @RequestParam(value = "body") String body) {
        log.info("支付宝支付入参：{}", amt + "," + subject + "," + body);
        String serverUrl = "https://openapi.alipaydev.com/gateway.do";
        String format = "json";
        String charset = "utf-8";
        String signType = "RSA2";
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey, format, charset,publicKey, signType);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 设置请求参数
        request.setReturnUrl(""); // 设置 支付宝同步返回支付结果url,可以以此结果做后续业务逻辑处理 可为空
		request.setNotifyUrl(""); // 设置 异步通知地址 可为空
        // 设置网页支付参数
        // outTradeNo 格式为yyyyMMddHHmmsss
        request.setBizContent("{\"out_trade_no\":\""+ new SimpleDateFormat("yyyyMMddHHmmsss").format(new Date()) +"\","
                + "\"total_amount\":\""+ amt +"\","
                + "\"subject\":\""+ "测试" +"\","
                + "\"body\":\""+ "iphone 12 pro max" +"\","
                + "\"timeout_express\":\""+ "5m" +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            if(response.isSuccess()){
               log.info("【 Ali pay调用成功 】");
            } else {
                log.info("【 Ali pay调用失败 】");
            }
            log.info("支付宝支付结束，响应为：{}", JSON.toJSON(response));
            // 就是orderString 可以直接给客户端请求，无需再做处理。
            return response.getBody();
        } catch (AlipayApiException e) {
            log.error("【 Ali pay 异常 】", e);
        }
        return "fail";
    }

}
